package com.zzw.concurrency;

import java.util.concurrent.Semaphore;

import org.junit.Test;

public class SemaphoreTest {

	private static Semaphore semaphore = new Semaphore(3);
	
	private Integer i = 0;

	private static Integer count = 0;

	public void run() throws InterruptedException {
		for(;i < 10; i++){

			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						semaphore.acquire();
						count ++;
						Thread.sleep(1000);
						System.out.println(Thread.currentThread().getName() + ":count:" + count);
						semaphore.release();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
		}
		//Thread.sleep(50000);
		System.out.println(count);
	}

	public static void main(String[] args) throws InterruptedException {
		new SemaphoreTest().run();
	}
}